#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <stdlib.h>
#include <pthread.h>
#include "shmem.h"
#include "msg_queue_peer.h"
struct shm_param shm_para;
struct std_node *shm_addr;
static int *total;                // 指向共享内存中数据节点总个数
static struct std_node *node_arr; // 指向共享内存中节点缓存数组头

#define N 1024

#define ERR_MSG(msg)                           \
    do                                         \
    {                                          \
        fprintf(stderr, "line:%d ", __LINE__); \
        perror(msg);                           \
    } while (0)

#define MAX_NODE 128 // 最大支持学生数目
#define STM_NODE_LEN sizeof(struct stm32)
#define MAX_NODE_SIZE (MAX_NODE * STM_NODE_LEN)

int main(int argc, char *argv[])
{
    // 初始化共享内存

    const char *shm_name = "daxaingmugongxiang";
    size_t shm_size = sizeof(struct std_node); // 根据您的需要设置共享内存的大小

    if (shm_init(&shm_para, shm_name, shm_size) != 0)
    {
        ERR_MSG("Failed to initialize shared memory");
        return -1;
    }

    // 获取共享内存地址
    void *shm_addr = shm_getaddr(&shm_para);
    if (shm_addr == NULL)
    {
        ERR_MSG("Failed to get shared memory address");
        return -1;
    }

    total = (int *)shm_addr;
    node_arr = (struct std_node *)(shm_addr + sizeof(int));

    printf("11111\n");
    while (1)
    {
        for (int i = 0; i < *total; i++)
        {
            printf("%d\n", *total);
            // printf("22222\n");

            if (node_arr[i].type == 3)
            {
                printf("key=%d, type=%d,value=%.4f\n",
                       node_arr[i].key,
                       node_arr[i].type,
                       node_arr[i].new_val.b);
            }
            else if (node_arr[i].type == 1)
            {
                printf("key=%d,type=%d,value=%d\n",
                       node_arr[i].key,
                       node_arr[i].type,
                       node_arr[i].new_val.bool);
            }
        }
        printf("a\n");
        sleep(2);
    }

    // 这个接口只有在这块共享内存不用了才能删除，项目中如果一直使用，不要调用这个接口
    shm_del(&shm_para);

    return 0;
}
